﻿// ***********************************************************************
// Assembly         : MVC4Portal
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="PageManager_Content.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace InnovaApp.Portals.MVC4Portal.Controllers.FriendlyURLHandling
{
    /// <summary>
    /// Class PageManager_Content
    /// </summary>
    public class PageManager_Content : _PageManagerBase
    {
        //public static _ContentElementBase_MetaModel GetContentByFriendlyUrl(string friendlyURL)
        //{
        //    _ContentElementBase_MetaModel toReturn = new ContentElementPage_MetaModel();
        //    Structure_Element structureElement = null;
        //    Structure_Page EtyPage = null;
        //    string UrlPartTmp = string.Empty;
        //    GroupPurchasePortalRepositories _context = new GroupPurchasePortal.Layers.DataLayer.GroupPurchasePortalRepositories();
        //    using (_context)
        //    {
        //        if (friendlyURL == "~home~")
        //        {
        //            //Recherche de la page d'accueil
        //            //Recherche de la version active
        //            Structure_Version EtyStructureVersion = _context.Structure_Version_Rep.Query
        //            (
        //                x =>
        //                x.Application.Name == Membership.ApplicationName
        //                &&
        //                x.VersionDate < DateTime.Now
        //            ).OrderByDescending(x => x.VersionDate).First();
        //            if (EtyStructureVersion.FK_Structure_Page_Home.HasValue)
        //            {
        //                int pageId = EtyStructureVersion.FK_Structure_Page_Home.Value;
        //                EtyPage = _context.Structure_Page_Rep.Query
        //                (
        //                    x =>
        //                    x.Id == pageId
        //                ).First();
        //                if (EtyPage == null)
        //                {
        //                    throw new Exception(GroupPurchasePortal.App_GlobalResources.Errors.HomePageNotFound);
        //                }
        //                toReturn = new ContentElementPage_MetaModel()
        //                {
        //                    ContentLanguage = "fr",
        //                    ControllerName = "Home",
        //                    FullUrl = "/",
        //                    MetaAbstract = string.Empty,
        //                    MetaAuthor = string.Empty,
        //                    MetaCategory = "General",
        //                    MetaCopyright = "",
        //                    MetaDescription = EtyPage.Description,
        //                    MetaDistribution = string.Empty,
        //                    MetaIdentifierUrl = string.Empty,
        //                    MetaKeywords = EtyPage.Keywords,
        //                    MetaRating = string.Empty,
        //                    MetaReplyTo = string.Empty,
        //                    MetaRevisitAfter = string.Empty,
        //                    MetaRobot = string.Empty,
        //                    NoPage = EtyPage.SortIndex,
        //                    ContentId = EtyPage.Id,
        //                    PageTitle = EtyPage.Title,
        //                };
        //            }
        //            else
        //            {
        //                throw new Exception(GroupPurchasePortal.App_GlobalResources.Errors.Navigation_AddressNotFound);
        //            }
        //        }
        //        else
        //        {
        //            //tFriendlyURL[0] : langue (Ignorée pour le moment)
        //            //tFriendlyURL[tFriendlyURL.count-1] : nom page    
        //            //Recherche du MenuItem
        //            //tFriendlyURL.Length minimum 3

        //            List<string> FriendlyUrlPartCollection = friendlyURL.Split('/').ToList();
        //            switch (FriendlyUrlPartCollection.Count)
        //            {
        //                case 0:
        //                case 1:
        //                    throw new Exception(GroupPurchasePortal.App_GlobalResources.Errors.Navigation_AddressNotValid);
        //                    break;
        //                default:
        //                    int? parentId = null;
        //                    //for (var i = 1; i < FriendlyUrlPartCollection.Count - 1; i++)
        //                    for (var i = 1; i < FriendlyUrlPartCollection.Count; i++)
        //                    {
        //                        UrlPartTmp = FriendlyUrlPartCollection.ElementAt(i).ToLower();
        //                        if (!parentId.HasValue)
        //                        {
        //                            structureElement = _context.Structure_Element_Rep.Query
        //                                                        (x =>
        //                                                             x.Application.Name == Membership.ApplicationName
        //                                                             &&
        //                                                             x.NavigationUrlPart.ToLower() == UrlPartTmp
        //                                                             &&
        //                                                             x.Structure_Element2 == null
        //                                                        ).FirstOrDefault();
        //                        }
        //                        else
        //                        {
        //                            structureElement = _context.Structure_Element_Rep.Query
        //                                                        (x =>
        //                                                             x.Application.Name == Membership.ApplicationName
        //                                                             &&
        //                                                             x.NavigationUrlPart.ToLower() == UrlPartTmp
        //                                                             &&
        //                                                             x.Structure_Element2.Id == parentId.Value
        //                                                        ).FirstOrDefault();
        //                        }
        //                        if (structureElement == null)
        //                        {
        //                            throw new Exception(GroupPurchasePortal.App_GlobalResources.Errors.Navigation_AddressNotFound);
        //                        }
        //                        else
        //                        {
        //                            parentId = structureElement.Id;
        //                        }
        //                    }
        //                    //Analyse du type d'élément attendu (Page, Brique logicielle, boutique ?)
        //                    var structureElementType = EnumTools.EnumTypeTo<_Structure_Base.enStructureElementTypes>(structureElement.FK_StructureElementType);
        //                    //Recherche de la page rattachée à cet élément

        //                    UrlPartTmp = FriendlyUrlPartCollection.ElementAt(FriendlyUrlPartCollection.Count - 1);

        //                    switch (structureElementType)
        //                    {
        //                        case _Structure_Base.enStructureElementTypes.Folder:
        //                            toReturn = GetPage_NoContentYet(friendlyURL, FriendlyUrlPartCollection);
        //                            break;
        //                        case _Structure_Base.enStructureElementTypes.Store_Home:
        //                            toReturn = GetPage_StoreHome(_context, friendlyURL);
        //                            break;
        //                        case _Structure_Base.enStructureElementTypes.Store_Deal:
        //                            break;
        //                        default:
        //                            throw new NotImplementedException();
        //                    }

        //                    EtyPage = _context.Structure_Page_Rep.Query
        //                        (x =>
        //                            x.FK_StructureElement == parentId.Value
        //                            &&
        //                            x.NavigationURL.ToLower() == UrlPartTmp
        //                        ).FirstOrDefault();

        //                    if (EtyPage == null)
        //                    {
        //                        toReturn = new ContentElementPage_MetaModel()
        //                        {
        //                            ContentLanguage = FriendlyUrlPartCollection.ElementAt(0),
        //                            ControllerName = "Home",
        //                            FullUrl = string.Format(@"/{0}", friendlyURL),
        //                            MetaAbstract = string.Empty,
        //                            MetaAuthor = string.Empty,
        //                            MetaCategory = string.Empty,
        //                            MetaCopyright = string.Empty,
        //                            MetaDescription = string.Empty,
        //                            MetaDistribution = string.Empty,
        //                            MetaIdentifierUrl = string.Empty,
        //                            MetaKeywords = string.Empty,
        //                            MetaRating = string.Empty,
        //                            MetaReplyTo = string.Empty,
        //                            MetaRevisitAfter = string.Empty,
        //                            MetaRobot = string.Empty,
        //                            NoPage = 1,
        //                            ContentId = -1,
        //                            PageTitle = string.Empty,
        //                            Content_Html = App_GlobalResources.Errors.Navigation_NoContentYet
        //                        };
        //                    }
        //                    else
        //                    {
        //                        toReturn = new ContentElementPage_MetaModel()
        //                        {
        //                            ContentLanguage = FriendlyUrlPartCollection.ElementAt(0),
        //                            ControllerName = "Home",
        //                            FullUrl = string.Format(@"/{0}", friendlyURL),
        //                            MetaAbstract = string.Empty,
        //                            MetaAuthor = string.Empty,
        //                            MetaCategory = "General",
        //                            MetaCopyright = "",
        //                            MetaDescription = EtyPage.Description,
        //                            MetaDistribution = string.Empty,
        //                            MetaIdentifierUrl = string.Empty,
        //                            MetaKeywords = EtyPage.Keywords,
        //                            MetaRating = string.Empty,
        //                            MetaReplyTo = string.Empty,
        //                            MetaRevisitAfter = string.Empty,
        //                            MetaRobot = string.Empty,
        //                            NoPage = EtyPage.SortIndex,
        //                            ContentId = EtyPage.Id,
        //                            PageTitle = EtyPage.Title,
        //                            Content_Html = EtyPage.PageContent_Html,
        //                            Resume_Html = EtyPage.ResumeContent_Html
        //                        };
        //                    }
        //                    break;
        //            }
        //        }
        //    }
        //    return toReturn;
        //}

        ///// <summary>
        ///// L'élément adressé est la racine d'un dossier mais ce dernier ne contient pas encore de page
        ///// ToDo : Réfléchir s'il faut proposer une navigation vers la home ou s'il faut rediriger automatiquement 
        ///// vers la home avec un message
        ///// </summary>
        ///// <returns></returns>
        //private static ContentElementPage_MetaModel GetPage_NoContentYet(string friendlyURL, List<string> friendlyUrlPartCollection)
        //{
        //    ContentElementPage_MetaModel toReturn =
        //    new ContentElementPage_MetaModel()
        //    {
        //        ContentLanguage = friendlyUrlPartCollection.ElementAt(0),
        //        ControllerName = "Home",
        //        FullUrl = string.Format(@"/{0}", friendlyURL),
        //        MetaAbstract = string.Empty,
        //        MetaAuthor = string.Empty,
        //        MetaCategory = string.Empty,
        //        MetaCopyright = string.Empty,
        //        MetaDescription = string.Empty,
        //        MetaDistribution = string.Empty,
        //        MetaIdentifierUrl = string.Empty,
        //        MetaKeywords = string.Empty,
        //        MetaRating = string.Empty,
        //        MetaReplyTo = string.Empty,
        //        MetaRevisitAfter = string.Empty,
        //        MetaRobot = string.Empty,
        //        NoPage = 1,
        //        ContentId = -1,
        //        PageTitle = string.Empty,
        //        Content_Html = App_GlobalResources.Errors.Navigation_NoContentYet
        //    };
        //    return toReturn;
        //}




        //private static _ContentElementBase_MetaModel GetPage_StoreHome(GroupPurchasePortalRepositories _context, string friendlyUrl)
        //{
        //    ContentElementStoreIndex_MetaModel toReturn = new ContentElementStoreIndex_MetaModel();
        //    Deal currentDeal;
        //    List<Deal> deals = _context.Deal_Rep.Query
        //    (
        //        x =>
        //            x.Application.Name == Membership.ApplicationName
        //            &&
        //            x.Date_Launch < DateTime.Now
        //            &&
        //            x.Date_End > DateTime.Now
        //            &&
        //            (
        //                x.TodayDeal_History.Where
        //                (
        //                    y => y.Date_Effect < DateTime.Now
        //                ).OrderByDescending
        //                (
        //                    z => z.Date_Effect
        //                ).FirstOrDefault() != null
        //            )
        //    )
        //    .Take(4).ToList();
        //    if (deals != null)
        //    {
        //        if (deals.Count >= 1)
        //        {
        //            MembershipUser currentUser = Membership.GetUser();
        //            currentDeal = deals.First();
        //            toReturn.ContentId = currentDeal.Id;
        //            toReturn.ActionName = "Index";
        //            toReturn.ControllerName = "StoreHome";
        //            toReturn.Content_Html = currentDeal.Content_HTML_Page;
        //            toReturn.FullUrl = friendlyUrl;
        //            toReturn.MetaAbstract = string.Empty; //ToDo Réfléchir à quoi mettre ici
        //            //if (author == null)
        //            //{
        //            toReturn.MetaAuthor = string.Empty; //ToDo : Gérer l'auteur pour les deals
        //            //}
        //            //else
        //            //{
        //            //    toReturn.MetaAuthor = string.Format(@"{0} {1}", author.FirstName, author.LastName);
        //            //}
        //            toReturn.MetaCategory = "Commerce"; //ToDo : Passer par les ressources
        //            toReturn.MetaCopyright = _context.ClientSite_Details_Rep.Query
        //                (x => x.Application.Name == Membership.ApplicationName).First().Copyright;
        //            toReturn.MetaDescription = currentDeal.Content_Description;
        //            toReturn.MetaDistribution = string.Empty; //ToDo : Réfléchir à quoi mettre ici
        //            toReturn.MetaIdentifierUrl = HttpContext.Current.Request.Url.Authority;
        //            toReturn.MetaKeywords = currentDeal.Content_Keywords;
        //            toReturn.MetaRating = GroupPurchasePortal.Layers.CommonLayer.HTMLHelpers._HTMLHelpers_Base.enMetaRatingOptions.General.HTMLProperties().HTMLValue;
        //            //toReturn.MetaReplyTo = _context.User_Details_Rep.Query(x => x.Id == (Guid)Membership.GetUser().ProviderUserKey).Single().User.Email;
        //            toReturn.MetaReplyTo = string.Empty; //ToDo : Gérer le ReplyTo des pages dans le Sites détails
        //            toReturn.MetaRevisitAfter = GroupPurchasePortal.Layers.CommonLayer.HTMLHelpers._HTMLHelpers_Base.enMetaRevisitAfter.FiveteenDays.HTMLProperties().HTMLValue;
        //            toReturn.MetaRobot = GroupPurchasePortal.Layers.CommonLayer.HTMLHelpers._HTMLHelpers_Base.enMetaRobots.Index_Follow.HTMLProperties().HTMLValue;
        //            toReturn.NoPage = 1;
        //            toReturn.PageTitle = currentDeal.Title;
        //            toReturn.PimaryDeal = new Models.MetaModels.Deal_MetaModel()
        //            {
        //                deal = new Models.Deal_MetaData()
        //                {
        //                    Content_Description = currentDeal.Content_Description,
        //                    Content_HTML_Page = currentDeal.Content_HTML_Page,
        //                    Content_HTML_Resume = currentDeal.Content_HTML_Resume,
        //                    Content_Illustration_Big = currentDeal.Content_Illustration_Big,
        //                    Content_Illustration_Small = currentDeal.Content_Illustration_Small,
        //                    Content_Keywords = currentDeal.Content_Keywords,
        //                    Content_Text_Page = currentDeal.Content_Text_Page,
        //                    Content_Text_Resume = currentDeal.Content_Text_Resume,
        //                    Date_Creation = currentDeal.Date_Creation,
        //                    Date_End = currentDeal.Date_End,
        //                    Date_Launch = currentDeal.Date_Launch,
        //                    Date_Validity_Begin = currentDeal.Date_Validity_Begin,
        //                    Date_Validity_End = currentDeal.Date_Validity_End,
        //                    FK_Application = currentDeal.FK_Application,
        //                    FK_Deal_Type = currentDeal.FK_Deal_Type,
        //                    FK_Partner = currentDeal.FK_Partner,
        //                    FriendlyURL = currentDeal.FriendlyURL,
        //                    Id = currentDeal.Id,
        //                    StockAlert = currentDeal.StockAlert,
        //                    Title = currentDeal.Title
        //                },
        //                dealTargetPrice = new DealTargetPrice_MetaData()
        //                {
        //                    FK_Deal = currentDeal.DealTargetPrices.First().FK_Deal,
        //                    Id = currentDeal.DealTargetPrices.First().Id,
        //                    Price = currentDeal.DealTargetPrices.First().Price,
        //                    RequiredClientsCount = currentDeal.DealTargetPrices.First().RequiredClientsCount
        //                }
        //            };
        //            toReturn.SecondaryDeals = deals.Skip(1).Select
        //            (
        //                x =>
        //                new Deal_MetaData()
        //                {
        //                    Content_Description = x.Content_Description,
        //                    Content_HTML_Page = x.Content_HTML_Page,
        //                    Content_HTML_Resume = x.Content_HTML_Resume,
        //                    Content_Illustration_Big = x.Content_Illustration_Big,
        //                    Content_Illustration_Small = x.Content_Illustration_Small,
        //                    Content_Keywords = x.Content_Keywords,
        //                    Content_Text_Page = x.Content_Text_Page,
        //                    Content_Text_Resume = x.Content_Text_Resume,
        //                    Date_Creation = x.Date_Creation,
        //                    Date_End = x.Date_End,
        //                    Date_Launch = x.Date_Launch,
        //                    Date_Validity_Begin = x.Date_Validity_Begin,
        //                    Date_Validity_End = x.Date_Validity_End,
        //                    FK_Application = x.FK_Application,
        //                    FK_Deal_Type = x.FK_Deal_Type,
        //                    FK_Partner = x.FK_Partner,
        //                    FriendlyURL = x.FriendlyURL,
        //                    Id = x.Id,
        //                    StockAlert = x.StockAlert,
        //                    Title = x.Title
        //                }
        //            ).ToList();
        //            toReturn.StructureElementType = _Structure_Base.enStructureElementTypes.Store_Home;
        //        }
        //    }
        //    return toReturn;
        //}

    }
}